/*
 *    This file is part of SSHTunneler.
 *
 *    Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
 *
 *    SSHTunneler is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    SSHTunneler is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with SSHTunneler.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package sshtunneler.client.gui;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import sshtunneler.client.data.Config;
import sshtunneler.client.tray.SysTrayManager;
import sshtunneler.library.DefaultPort;
import sshtunneler.library.DefaultPorts;

public class SetConfig extends javax.swing.JDialog {
	private static final long serialVersionUID = -4773727179486036472L;

	SetConfig(JFrame mainFrame) {
		super(mainFrame, true);
		this.initComponents();
		this.txt_default_username.setText(Config.default_username);
		this.chk_auto_save_config.setSelected(Config.auto_save_options);
		this.chk_auto_save_servers
				.setSelected(Config.auto_save_servers_and_tunnel);
		this.chk_start_minimized.setSelected(Config.start_minimized);
		this.chk_connect_to_all_on_start
				.setSelected(Config.connect_to_all_on_start);
		this.chk_start_minimized
				.setEnabled(SysTrayManager.inst.can_use_systray);
		this.populate_list();
	}

	private void initComponents() {

		this.jTabbedPane1 = new javax.swing.JTabbedPane();
		this.panel_general = new javax.swing.JPanel();
		this.jPanel3 = new javax.swing.JPanel();
		this.jLabel1 = new javax.swing.JLabel();
		this.txt_default_username = new javax.swing.JTextField();
		this.chk_auto_save_servers = new javax.swing.JCheckBox();
		this.chk_auto_save_config = new javax.swing.JCheckBox();
		this.chk_start_minimized = new javax.swing.JCheckBox();
		this.chk_connect_to_all_on_start = new javax.swing.JCheckBox();
		this.panel_service = new javax.swing.JPanel();
		this.jScrollPane1 = new javax.swing.JScrollPane();
		this.lst_ports = new javax.swing.JList();
		this.jPanel2 = new javax.swing.JPanel();
		this.cmd_add_port = new javax.swing.JButton();
		this.cmd_remove_port = new javax.swing.JButton();
		this.jPanel1 = new javax.swing.JPanel();
		this.cmd_cancel = new javax.swing.JButton();
		this.cmd_save = new javax.swing.JButton();

		this
				.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.setTitle("Configure");
		this.setName("Form");
		this.setResizable(false);

		this.jTabbedPane1.setName("jTabbedPane1");

		this.panel_general.setName("panel_general");
		this.panel_general.setLayout(new java.awt.GridLayout(5, 1));

		this.jPanel3.setName("jPanel3");

		this.jLabel1.setText("Default Username:");
		this.jLabel1.setName("jLabel1");
		this.jPanel3.add(this.jLabel1);

		this.txt_default_username
				.setMaximumSize(new java.awt.Dimension(80, 20));
		this.txt_default_username
				.setMinimumSize(new java.awt.Dimension(80, 20));
		this.txt_default_username.setName("txt_default_username");
		this.txt_default_username.setPreferredSize(new java.awt.Dimension(80,
				20));
		this.jPanel3.add(this.txt_default_username);

		this.panel_general.add(this.jPanel3);

		this.chk_auto_save_servers.setText("Auto-Save servers/tunnels");
		this.chk_auto_save_servers
				.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
		this.chk_auto_save_servers.setName("chk_auto_save_servers");
		this.panel_general.add(this.chk_auto_save_servers);

		this.chk_auto_save_config.setText("Auto-Save configuration");
		this.chk_auto_save_config
				.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
		this.chk_auto_save_config.setName("chk_auto_save_config");
		this.panel_general.add(this.chk_auto_save_config);

		this.chk_start_minimized.setText("Start minimized");
		this.chk_start_minimized
				.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
		this.chk_start_minimized.setName("chk_start_minimized");
		this.panel_general.add(this.chk_start_minimized);

		this.chk_connect_to_all_on_start
				.setText("Connect on all server on startup");
		this.chk_connect_to_all_on_start
				.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
		this.chk_connect_to_all_on_start.setName("chk_connect_to_all_on_start");
		this.panel_general.add(this.chk_connect_to_all_on_start);

		this.jTabbedPane1.addTab("General", this.panel_general);

		this.panel_service.setMaximumSize(new java.awt.Dimension(300, 300));
		this.panel_service.setMinimumSize(new java.awt.Dimension(250, 46));
		this.panel_service.setName("panel_service");
		this.panel_service.setPreferredSize(new java.awt.Dimension(250, 159));
		this.panel_service.setLayout(new javax.swing.BoxLayout(
				this.panel_service, javax.swing.BoxLayout.LINE_AXIS));

		this.jScrollPane1.setMaximumSize(new java.awt.Dimension(200, 300));
		this.jScrollPane1.setName("jScrollPane1");

		this.lst_ports.setModel(new javax.swing.AbstractListModel() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 8597366077418810478L;
			String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4",
					"Item 5" };

			public int getSize() {
				return this.strings.length;
			}

			public Object getElementAt(int i) {
				return this.strings[i];
			}
		});
		this.lst_ports.setName("lst_ports");
		this.jScrollPane1.setViewportView(this.lst_ports);

		this.panel_service.add(this.jScrollPane1);

		this.jPanel2.setMaximumSize(new java.awt.Dimension(80, 50));
		this.jPanel2.setMinimumSize(new java.awt.Dimension(80, 50));
		this.jPanel2.setName("jPanel2");
		this.jPanel2.setPreferredSize(new java.awt.Dimension(80, 50));
		this.jPanel2.setLayout(new javax.swing.BoxLayout(this.jPanel2,
				javax.swing.BoxLayout.Y_AXIS));

		this.cmd_add_port.setText("Add");
		this.cmd_add_port.setMaximumSize(new java.awt.Dimension(80, 23));
		this.cmd_add_port.setMinimumSize(new java.awt.Dimension(80, 23));
		this.cmd_add_port.setName("cmd_add_port");
		this.cmd_add_port.setPreferredSize(new java.awt.Dimension(80, 23));
		this.cmd_add_port
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						SetConfig.this.cmd_add_portActionPerformed(evt);
					}
				});
		this.jPanel2.add(this.cmd_add_port);

		this.cmd_remove_port.setText("Remove");
		this.cmd_remove_port.setMaximumSize(new java.awt.Dimension(80, 25));
		this.cmd_remove_port.setMinimumSize(new java.awt.Dimension(80, 25));
		this.cmd_remove_port.setName("cmd_remove_port");
		this.cmd_remove_port.setPreferredSize(new java.awt.Dimension(80, 25));
		this.cmd_remove_port
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						SetConfig.this.cmd_remove_portActionPerformed(evt);
					}
				});
		this.jPanel2.add(this.cmd_remove_port);

		this.panel_service.add(this.jPanel2);

		this.jTabbedPane1.addTab("Services", this.panel_service);

		this.getContentPane().add(this.jTabbedPane1,
				java.awt.BorderLayout.CENTER);

		this.jPanel1.setMaximumSize(new java.awt.Dimension(300, 33));
		this.jPanel1.setName("jPanel1");
		this.jPanel1.setPreferredSize(new java.awt.Dimension(300, 33));

		this.cmd_cancel.setText("Cancel");
		this.cmd_cancel.setName("cmd_cancel");
		this.cmd_cancel.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				SetConfig.this.cmd_cancelActionPerformed(evt);
			}
		});
		this.jPanel1.add(this.cmd_cancel);

		this.cmd_save.setText("Save");
		this.cmd_save.setName("cmd_save");
		this.cmd_save.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				SetConfig.this.cmd_saveActionPerformed(evt);
			}
		});
		this.jPanel1.add(this.cmd_save);

		this.getContentPane().add(this.jPanel1, java.awt.BorderLayout.SOUTH);

		this.pack();
	}

	private void cmd_saveActionPerformed(java.awt.event.ActionEvent evt) {
		Config.default_username = this.txt_default_username.getText();
		Config.auto_save_options = this.chk_auto_save_config.isSelected();
		Config.auto_save_servers_and_tunnel = this.chk_auto_save_servers
				.isSelected();
		Config.start_minimized = this.chk_start_minimized.isSelected();
		Config.connect_to_all_on_start = this.chk_connect_to_all_on_start
				.isSelected();

		if (Config.auto_save_options) {
			Config.inst.auto_save();
		}

		this.dispose();
	}

	private void cmd_cancelActionPerformed(java.awt.event.ActionEvent evt) {
		this.dispose();
	}

	private void cmd_add_portActionPerformed(java.awt.event.ActionEvent evt) {
		JPanel add_panel = new JPanel(new GridLayout(2, 2));
		JTextField txt_port = new JTextField("");
		JTextField txt_name = new JTextField("");

		add_panel.add(new JLabel("Port:"));
		add_panel.add(txt_port);
		add_panel.add(new JLabel("Name:"));
		add_panel.add(txt_name);

		boolean ended = false;
		String name = "";
		String str_port;
		int port = -1;

		while (!ended) {
			port = -1;
			int ret = JOptionPane.showOptionDialog(null, add_panel,
					"Please provide these infomations: ",
					JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
					null, null, null);

			if (ret != JOptionPane.OK_OPTION) {
				return;
			}
			ended = true;
			name = txt_name.getText();
			str_port = txt_port.getText();

			txt_name.setBorder(BorderFactory.createLineBorder(Color.BLACK));
			txt_port.setBorder(BorderFactory.createLineBorder(Color.BLACK));

			if (name == null || name.equals("")) {
				JOptionPane.showMessageDialog(null,
						"You must provide a name for this port");
				txt_name.setBorder(BorderFactory.createLineBorder(Color.RED));
				ended = false;
			}
			if (str_port == null || str_port.equals("")) {
				JOptionPane.showMessageDialog(null, "You must provide a port");
				txt_port.setBorder(BorderFactory.createLineBorder(Color.RED));
				ended = false;
			} else {
				try {
					port = Integer.parseInt(str_port);
					if (port < 0) {
						JOptionPane
								.showMessageDialog(null, "Port must be >= 0");
						txt_port.setBorder(BorderFactory
								.createLineBorder(Color.RED));
						ended = false;
					}
				} catch (NumberFormatException ex) {
					JOptionPane.showMessageDialog(null,
							"Port must be an integer");
					txt_port.setBorder(BorderFactory
							.createLineBorder(Color.RED));
					ended = false;
				}
			}
		}

		((DefaultListModel) this.lst_ports.getModel())
				.addElement(DefaultPorts.inst.add_port(name, port));
	}

	private void cmd_remove_portActionPerformed(java.awt.event.ActionEvent evt) {
		if (this.lst_ports.getSelectedValue() == null) {
			return;
		}
		if (!(this.lst_ports.getSelectedValue() instanceof DefaultPort)) {
			return;
		}

		DefaultPort selected = (DefaultPort) this.lst_ports.getSelectedValue();
		DefaultPorts.inst.remove_port(selected);
		((DefaultListModel) this.lst_ports.getModel()).removeElement(selected);
	}

	private javax.swing.JCheckBox chk_auto_save_config;
	private javax.swing.JCheckBox chk_auto_save_servers;
	private javax.swing.JCheckBox chk_connect_to_all_on_start;
	private javax.swing.JCheckBox chk_start_minimized;
	private javax.swing.JButton cmd_add_port;
	private javax.swing.JButton cmd_cancel;
	private javax.swing.JButton cmd_remove_port;
	private javax.swing.JButton cmd_save;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JPanel jPanel3;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTabbedPane jTabbedPane1;
	private javax.swing.JList lst_ports;
	private javax.swing.JPanel panel_general;
	private javax.swing.JPanel panel_service;
	private javax.swing.JTextField txt_default_username;

	private void populate_list() {
		DefaultListModel model = new DefaultListModel();
		this.lst_ports.setModel(model);

		for (DefaultPort p : DefaultPorts.inst.get_all_ports()) {
			model.addElement(p);
		}
	}
}
